//
//  ZJDesThirdTravelViewController.m
//  BreadTravel
//
//  Created by 张健 on 15/12/24.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZJDesThirdTravelViewController.h"
#import "ZJDesThirdTravelCollectionViewCell.h"

@interface ZJDesThirdTravelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *allArray;

@property(nonatomic,retain)UIView *headView;
@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)NSMutableArray *arrayTitle;

@end

@implementation ZJDesThirdTravelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self.tabBarController.tabBar setHidden:YES];
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    [self.view addSubview:self.headView];
    
    for (int i = 0; i < 5; i++) {
        UIButton *allBut = [UIButton buttonWithType:UIButtonTypeCustom];
        allBut.frame = CGRectMake(10 +i*(WIDTH/5), 5, WIDTH/5-20, 27);
        allBut.layer.masksToBounds = YES;
        allBut.tag = 1000+i;
        [allBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        allBut.layer.cornerRadius = 13;
        allBut.backgroundColor = [UIColor colorWithRed:0.970 green:0.970 blue:0.920 alpha:1.000];
        [allBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.headView addSubview:allBut];
    }
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置每个item的大小
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT -40);
    
    //设置最小行间距
    flowLayout.minimumLineSpacing = 0;
    //    //设置最小列间距
    flowLayout.minimumInteritemSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT-40) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[ZJDesThirdTravelCollectionViewCell class] forCellWithReuseIdentifier:@"ZJDesThirdTravelCollectionViewCell"];
    
    self.arrayTitle = [NSMutableArray array];
    self.arrayTitle = [NSMutableArray arrayWithObjects:@"all", @"sights", @"hotel", @"restaurant", @"experience", nil];
    

    
}

-(void)viewWillAppear:(BOOL)animated{
    
    
    UIButton *buttonAll = [self.view viewWithTag:1000];
    [buttonAll setTitle:@"全部" forState:UIControlStateNormal];
    [buttonAll setTitleColor:[UIColor colorWithRed:0.970 green:0.970 blue:0.920 alpha:1.000] forState:UIControlStateNormal];
    buttonAll.backgroundColor = [UIColor blueColor];
    
    UIButton *buttonSights = [self.view viewWithTag:1001];
    [buttonSights setTitle:@"景点" forState:UIControlStateNormal];
    
    UIButton *buttonHotel = [self.view viewWithTag:1002];
    [buttonHotel setTitle:@"住宿" forState:UIControlStateNormal];
    
    UIButton *buttonRestaurant = [self.view viewWithTag:1003];
    [buttonRestaurant setTitle:@"餐厅" forState:UIControlStateNormal];
    
    UIButton *buttonExperience = [self.view viewWithTag:1004];
    [buttonExperience setTitle:@"娱乐" forState:UIControlStateNormal];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"ZJDesThirdTravelCollectionViewCell";
    ZJDesThirdTravelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.type = self.type;
    cell.title = [self.arrayTitle objectAtIndex:indexPath.row];
    cell.id = self.id;
    cell.paramete = [self.arrayTitle objectAtIndex:indexPath.row];
    return cell;
}

- (void)buttonAction:(UIButton *)but{
    
    for (int i = 0; i < 5; i++) {
        UIButton *buttonNow = [self.view viewWithTag:1000+i];
        buttonNow.backgroundColor = [UIColor colorWithRed:0.970 green:0.970 blue:0.920 alpha:1.000];;
        [buttonNow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    but.backgroundColor = [UIColor blueColor];
    [but setTitleColor:[UIColor colorWithRed:0.970 green:0.970 blue:0.920 alpha:1.000] forState:UIControlStateNormal];
    
    if (but.tag == 1000) {
        [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else if (but.tag == 1001) {
        [self.collectionView setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
    }else if (but.tag == 1002){
        [self.collectionView setContentOffset:CGPointMake(WIDTH*2, 0) animated:YES];
    }else if (but.tag == 1003){
        [self.collectionView setContentOffset:CGPointMake(WIDTH*3, 0) animated:YES];
    }else{
        [self.collectionView setContentOffset:CGPointMake(WIDTH*4, 0) animated:YES];
    }
    
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    for (int i = 0; i < 5; i++) {
        UIButton *buttonNow = [self.view viewWithTag:1000+i];
        buttonNow.backgroundColor = [UIColor colorWithRed:0.970 green:0.970 blue:0.920 alpha:1.000];
        [buttonNow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    UIButton *buttonNow = [self.view viewWithTag:1000+scrollView.contentOffset.x/WIDTH];
    buttonNow.backgroundColor = [UIColor blueColor];
    [buttonNow setTitleColor:[UIColor colorWithRed:0.970 green:0.970 blue:0.920 alpha:1.000] forState:UIControlStateNormal];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrayTitle.count;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
